package com.github.ilioili.justdoit.components.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.github.ilioili.justdoit.components.fragment.QuadrantScheduleEventListFragment;
import com.github.ilioili.justdoit.R;
import com.github.ilioili.justdoit.common.AppBaseActivity;
import com.github.ilioili.justdoit.common.config.EventConstants;
import com.github.ilioili.justdoit.model.dao.ScheduleEventDao;
import com.github.ilioili.justdoit.model.pojo.ScheduleEvent;
import com.github.ilioili.justdoit.util.CompatUtil;
import com.github.ilioili.justdoit.util.sql.SqlCallback;
import com.taihe.template.base.injection.Id;
import com.taihe.template.base.injection.Layout;

import java.util.List;

/**
 * Created by hantuo on 16/5/15.
 */
@Layout(R.layout.activity_schedule_event_tab)
public class QuadrantActivity extends AppBaseActivity {

    @Id(R.id.viewPager)
    private ViewPager viewPager;
    @Id(R.id.toolbar)
    private Toolbar toolbar;
    @Id(R.id.tabLayout)
    private TabLayout tabLayout;
    private int[] titles = new int[]{R.string.quadrant1, R.string.quadrant2, R.string.quadrant3, R.string.quadrant4};

    QuadrantScheduleEventListFragment quadrant1Fragment = QuadrantScheduleEventListFragment.newInstance(QuadrantScheduleEventListFragment.Type.TODAY);
    QuadrantScheduleEventListFragment quadrant2Fragment = QuadrantScheduleEventListFragment.newInstance(QuadrantScheduleEventListFragment.Type.TOMORROW);
    QuadrantScheduleEventListFragment quadrant3Fragment = QuadrantScheduleEventListFragment.newInstance(QuadrantScheduleEventListFragment.Type.AFTERTOMORROW);
    QuadrantScheduleEventListFragment quadrant4Fragment = QuadrantScheduleEventListFragment.newInstance(QuadrantScheduleEventListFragment.Type.THISWEEK);

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.event_tab_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.menu_search) {
            startActivity(SearchActivity.newIntent(this));
            overridePendingTransition(0, 0);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return quadrant1Fragment;
                    case 1:
                        return quadrant2Fragment;
                    case 2:
                        return quadrant3Fragment;
                    default:
                        return quadrant4Fragment;
                }
            }

            @Override
            public int getCount() {
                return titles.length;
            }

            @Override
            public int getItemPosition(Object object) {
                return ViewPager.GONE;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return getResources().getString(titles[position]);
            }
        });
        tabLayout.setupWithViewPager(viewPager);
//        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                viewPager.setCurrentItem(tab.getPosition(), false);
//                switch (tab.getPosition()){
//                    case 0:
//                }
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        getFirstQuarentAndLoadData();
                        break;
                    case 1:
                        getSecondQuarentAndLoadData();
                        break;
                    case 2:
                        getThirdQuarentAndLoadData();
                        break;
                    default:
                        getFourthQuarentAndLoadData();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        getFirstQuarentAndLoadData();
    }

    @Override
    public void onResume() {
        super.onResume();
        switch (viewPager.getCurrentItem()) {
            case 0:
                getFirstQuarentAndLoadData();
                break;
            case 1:
                getSecondQuarentAndLoadData();
                break;
            case 2:
                getThirdQuarentAndLoadData();
                break;
            default:
                getFourthQuarentAndLoadData();
                break;
        }
    }

    private void getFirstQuarentAndLoadData() {
        ScheduleEventDao.getAllUncompleteEventByImportance(EventConstants.QUADRANT_IMPORTANT_AND_IMERGENCY, new SqlCallback<List<ScheduleEvent>>() {
            @Override
            public void onSucess(List<ScheduleEvent> scheduleEvents) {
                quadrant1Fragment.loadData(scheduleEvents);
                showOrHideNoContentBg(scheduleEvents);
            }
        });
    }

    private void getSecondQuarentAndLoadData() {
        ScheduleEventDao.getAllUncompleteEventByImportance(EventConstants.QUADRANT_NOT_IMPORTANT_BUT_IMERGENCY, new SqlCallback<List<ScheduleEvent>>() {
            @Override
            public void onSucess(List<ScheduleEvent> scheduleEvents) {
                quadrant2Fragment.loadData(scheduleEvents);
                showOrHideNoContentBg(scheduleEvents);
            }
        });
    }

    private void getThirdQuarentAndLoadData() {
        ScheduleEventDao.getAllUncompleteEventByImportance(EventConstants.QUADRANT_IMPORTANT_NOT_IMERGENCY, new SqlCallback<List<ScheduleEvent>>() {
            @Override
            public void onSucess(List<ScheduleEvent> scheduleEvents) {
                quadrant3Fragment.loadData(scheduleEvents);
                showOrHideNoContentBg(scheduleEvents);
            }
        });
    }

    private void getFourthQuarentAndLoadData() {
        ScheduleEventDao.getAllUncompleteEventByImportance(EventConstants.QUADRANT_NOT_IMPORTANT_NOT_IMERGENCY, new SqlCallback<List<ScheduleEvent>>() {
            @Override
            public void onSucess(List<ScheduleEvent> scheduleEvents) {
                quadrant4Fragment.loadData(scheduleEvents);
                showOrHideNoContentBg(scheduleEvents);
            }
        });
    }


    private void showOrHideNoContentBg(List<ScheduleEvent> scheduleEvents) {
        if (scheduleEvents.isEmpty()) {
            setVisiable(R.id.no_task);
        } else {
            setGone(R.id.no_task);
        }
    }

    public static Intent newIntent(Context context) {
        return new Intent(context, QuadrantActivity.class);
    }

    public void onClickNewEvent(View v) {
        int[] loc = new int[2];
        v.getLocationInWindow(loc);
        startActivity(CreateOrModifyActivity.newIntent(this, EventConstants.TYPE_EVENT, loc[0] + v.getWidth() / 2, loc[1] + v.getHeight() / 2, CompatUtil.getColor(this, R.color.colorAccent)));
        overridePendingTransition(0, 0);
    }


}
